/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.gk.common;

import cn.ac.iie.di.wx.gk.configuration.Configuration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.log4j.Logger;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    public static final String JETTY_SERVER = "jettyServer";
    public static final String JETTY_PORT = "jettyPort";
    public static final String JETTY_THREADNUM = "jettyThreadNum";
    public static final String LIMITINT = "limitint";
    public static final String TXURL = "txUrl";
    public static final String GXBURL = "gxbUrl";
    public static final String CmdQueryLimitValue = "CmdQueryLimitValue";
    public static final String CmdCommitLimitValue = "CmdCommitLimitValue";
    public static final String CmdAppealLimitValue = "CmdAppealLimitValue";
    public static final String commit = "commit";
    public static final String query = "query";
    public static final String appeal = "appeal";
    public static final String revoke = "revoke";
    
    public static AtomicInteger CmdQueryLimit = new AtomicInteger(0);
    public static AtomicInteger CmdCommitLimit = new AtomicInteger(0);
    public static AtomicInteger CmdAppealLimit = new AtomicInteger(0);

    public static AtomicInteger CmdQueryCount = new AtomicInteger(0);
    public static AtomicInteger CmdCommitCount = new AtomicInteger(0);
    public static AtomicInteger CmdAppealCount = new AtomicInteger(0);

    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    static Logger logger = Logger.getLogger(RuntimeEnv.class.getName());

    public static boolean initialize(Configuration pConf) throws Exception {

        if (pConf == null) {
            logger.error("configuration object is null");
            throw new Exception("configuration object is null");
        }

        conf = pConf;

        setKeyAndValue(JETTY_PORT, "jettyPort");
        setKeyAndValue(JETTY_SERVER, "jettyServer");
        setKeyAndValue(JETTY_THREADNUM, "jettyThreadNum");
        setKeyAndValue(LIMITINT, "limitint");
        setKeyAndValue(TXURL, "txUrl");
        setKeyAndValue(GXBURL, "gxbUrl");
        setKeyAndValue(CmdQueryLimitValue, "CmdQueryLimitValue");
        setKeyAndValue(CmdCommitLimitValue, "CmdCommitLimitValue");
        setKeyAndValue(CmdAppealLimitValue, "CmdAppealLimitValue");

        setKeyAndValue(commit, "commit");
        setKeyAndValue(query, "query");
        setKeyAndValue(appeal, "appeal");
        setKeyAndValue(revoke, "revoke");
        return true;
    }

    public static boolean setKeyAndValue(String KEY, String parameter) throws Exception {
        String value = conf.getString(KEY, "");
        if (value.isEmpty()) {
            logger.error("parameter " + parameter + " does not exist or is not defined");
            throw new Exception("parameter " + parameter + " does not exist or is not defined");
        }
        addParam(KEY, value);
        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
